package com.ncet.circuit.server;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.ncet.circuit.client.LoginService;
import com.ncet.circuit.shared.LoginInfo;

public class LoginServiceImpl extends RemoteServiceServlet implements LoginService
{
	private static final long serialVersionUID = 1L;

	public LoginInfo login( String successUrl, String failureUrl )
	{
		UserService userService = UserServiceFactory.getUserService( );
		User user = userService.getCurrentUser( );
		LoginInfo loginInfo = new LoginInfo( );

		if ( user != null )
		{
			loginInfo.setLoggedIn( true );
			loginInfo.setEmailAddress( user.getEmail( ) );
			loginInfo.setNickname( user.getNickname( ) );
			loginInfo.setLogoutUrl( userService.createLogoutURL( failureUrl ) );
		}
		else
		{
			loginInfo.setLoggedIn( false );
		}
		
		loginInfo.setLoginUrl( userService.createLoginURL( successUrl ) );
		loginInfo.setLogoutUrl( userService.createLogoutURL( failureUrl ) );
		return loginInfo;
	}

	@Override
	public String logout( String href )
	{
		UserService userService = UserServiceFactory.getUserService( );
		return userService.createLogoutURL( href );
	}

}
